﻿<UserControl x:Class="GSF.TimeSeries.Transport.UI.UserControls.SubscriberRequestUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="650" d:DesignWidth="850"
             xmlns:vm="clr-namespace:GSF.TimeSeries.Transport.UI.ViewModels"
             xmlns:uc="clr-namespace:GSF.TimeSeries.Transport.UI.UserControls"
             xmlns:ui="clr-namespace:GSF.TimeSeries.UI;assembly=GSF.TimeSeries.UI.WPF"
             Background="Transparent" Loaded="SubscriberRequestUserControl_Loaded" Unloaded="SubscriberRequestUserControl_Unloaded">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/GSF.TimeSeries.UI.WPF;Component/Resources/CommonResource.xaml" />
            </ResourceDictionary.MergedDictionaries>
            
            <vm:SubscriberRequestViewModel x:Key="ViewModel" />
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" DataContext="{StaticResource ViewModel}">
        <Border CornerRadius="5" BorderThickness="2" Background="White" BorderBrush="Red" Visibility="{Binding ConnectivityMessageVisibility}">
            <TextBlock HorizontalAlignment="Center">Unable to contact the service. Some features on this page may not be available.</TextBlock>
        </Border>
        
        <GroupBox Header="Publisher Info">
            <Grid HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                
                <Label Grid.Row="0" Grid.Column="0" Content="Acronym" />
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Mode=TwoWay, Path=PublisherAcronym, UpdateSourceTrigger=PropertyChanged}" CharacterCasing="Upper" />
                <Label Grid.Row="0" Grid.Column="2" Content="Name" />
                <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Mode=TwoWay, Path=PublisherName}" />

                <Label Grid.Row="1" Grid.Column="0" Content="Hostname" />
                <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Mode=TwoWay, Path=Hostname}" />
                <Label Grid.Row="1" Grid.Column="2" Content="Port" />
                <TextBox Grid.Row="1" Grid.Column="3" Width="{StaticResource XXSmallItemSize}" Text="{Binding Mode=TwoWay, Path=PublisherPort}" />

                <Label Grid.Row="2" Grid.Column="0" Content="Security mode" />
                <StackPanel Grid.Row="2" Grid.Column="1" Margin="5">
                    <RadioButton GroupName="SecurityMode" Content="TLS" IsChecked="True" Checked="SecurityModeRadioButton_Checked" />
                    <RadioButton GroupName="SecurityMode" Content="Gateway" Checked="SecurityModeRadioButton_Checked" />
                </StackPanel>
                
                <Label Grid.Row="2" Grid.Column="2" Content="UDP Port" />
                <StackPanel Grid.Row="2" Grid.Column="3" Orientation="Horizontal" VerticalAlignment="Top">
                    <TextBox Width="{StaticResource XXSmallItemSize}" Text="{Binding Mode=TwoWay, Path=UdpDataChannelPort}" IsEnabled="{Binding ElementName=UdpCheckBox, Path=IsChecked}" />
                    <CheckBox Name="UdpCheckBox" IsChecked="{Binding Mode=TwoWay, Path=UseUdpDataChannel}">
                        <TextBlock FontSize="10" FontStyle="Italic">
                            Leave unchecked for TCP only.
                        </TextBlock>
                    </CheckBox>
                </StackPanel>
            </Grid>
        </GroupBox>

        <GroupBox Header="Subscriber Info">
            <Grid HorizontalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" Content="Acronym" />
                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Mode=TwoWay, Path=SubscriberAcronym}" CharacterCasing="Upper"/>
                <Label Grid.Row="0" Grid.Column="2" Content="Name" />
                <TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Mode=TwoWay, Path=SubscriberName}" />

                <Label Grid.Row="1" Grid.Column="0" Content="Valid IP Addresses" />
                <TextBox Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="3" Width="{StaticResource XXXLargeItemSize}" Text="{Binding Mode=TwoWay, Path=ValidIPAddresses}" />
            </Grid>
        </GroupBox>

        <GroupBox Header="TLS" IsEnabled="{Binding TransportLayerSecuritySelected}">
            <StackPanel HorizontalAlignment="Center">
                <TextBlock Width="{StaticResource XXXLargeItemSize}" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap">
                    Remote certificate path should be the path on the server, relative to the trusted certificates path,
                    where the service will be able to locate the certificate. The browse button may not be relevant.
                </TextBlock>
                
                <Grid Margin="0,10,0,0" HorizontalAlignment="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    
                    <Label Grid.Row="0" Grid.Column="0" Content="Remote certificate" />
                    <StackPanel Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" Orientation="Horizontal">
                        <TextBox Width="{StaticResource XXLargeItemSize}" Text="{Binding Mode=TwoWay, Path=RemoteCertificateFile}" />
                        <Button Width="{StaticResource XSmallItemSize}" Height="25" Content="Browse..." Command="{Binding RemoteBrowseCommand}" />
                        <CheckBox Margin="10,0,0,0" Content="Self-signed?" IsChecked="{Binding Mode=TwoWay, Path=RemoteCertificateIsSelfSigned}" />
                    </StackPanel>

                    <Label Grid.Row="1" Grid.Column="0" Content="Valid Policy Errors" />
                    <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Mode=TwoWay, Path=ValidPolicyErrors}" />
                    <Label Grid.Row="1" Grid.Column="2" Content="Valid Chain Flags" />
                    <TextBox Grid.Row="1" Grid.Column="3" Text="{Binding Mode=TwoWay, Path=ValidChainFlags}" />

                    <Button Grid.Row="2" Grid.Column="3" Width="{StaticResource SmallerItemSize}" Height="25" Margin="10" HorizontalAlignment="Right" Content="Advanced..." Command="{Binding AdvancedTlsSettingsOpenCommand}" />
                </Grid>
            </StackPanel>
        </GroupBox>

        <ui:TsfPopup Name="AdvancedTlsSettingsPopup" Placement="Center" Topmost="False" IsOpen="{Binding AdvancedTlsSettingsPopupIsOpen}">
            <Border CornerRadius="5" BorderThickness="2" Background="White" BorderBrush="Blue">
                <StackPanel Name="CustomConfigurationPanel" Margin="5">
                    <GroupBox Header="Local certificate">
                        <StackPanel>
                            <TextBlock Width="{StaticResource XXXLargeItemSize}" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap">
                                By default, the Data Subscriber will use the self-signed certificate generated by the service at startup.
                                Use this interface to override that behavior. When setting the local certificate manually, you will need
                                to browse to a copy of the certificate, and also provide the path on the server where the service can
                                locate the certificate file.
                            </TextBlock>

                            <Label Content="Generate self-signed local certificate (optional):" FontSize="10" />
                            <uc:SelfSignedCertificateGenerator Margin="20,0,0,20" CertificateFile="{Binding Mode=TwoWay, Path=LocalCertificateFile}" ProcessException="SelfSignedCertificateGenerator_ProcessException" />

                            <Grid Name="LocalCertificateGrid">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <Label Grid.Row="0" Grid.Column="0" Content="Local certificate" />
                                <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                                    <TextBox Width="{StaticResource XXLargeItemSize}" Text="{Binding Mode=TwoWay, Path=LocalCertificateFile, UpdateSourceTrigger=PropertyChanged}" />
                                    <Button Height="25" Content="Browse..." Command="{Binding LocalBrowseCommand}" />
                                </StackPanel>

                                <Label Grid.Row="1" Grid.Column="0" Content="Server path" />
                                <TextBox Grid.Row="1" Grid.Column="1" Width="{StaticResource XXLargeItemSize}" Text="{Binding Mode=TwoWay, Path=LocalCertificateServerPath}" />
                            </Grid>
                        </StackPanel>
                    </GroupBox>

                    <GroupBox Header="Remote Certificate" Margin="0,10,0,10">
                        <StackPanel>
                            <TextBlock Width="{StaticResource XXXLargeItemSize}" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap">
                                This button will allow you to import a remote certificate to the trusted certificates directory
                                on the service so that you will be able to connect to the owner of this certificate.
                                Note that the certificate will not be imported until your authorization request has been created.
                            </TextBlock>
                            
                            <TextBlock HorizontalAlignment="Center" TextAlignment="Center" FontWeight="Bold" Text="{Binding ImportCertificatePath}" />
                            
                            <Button Width="{StaticResource SmallerItemSize}" Height="25" Content="Import CER..."
                                ToolTip="Imports a certificate into the trusted certificates directory on the server."
                                Command="{Binding ImportCertificateCommand}" />
                        </StackPanel>
                    </GroupBox>

                    <Button Width="{StaticResource XSmallItemSize}" Height="25" HorizontalAlignment="Right" Margin="10" Content="Close" Command="{Binding AdvancedTlsSettingsCloseCommand}" />
                </StackPanel>
            </Border>
        </ui:TsfPopup>
        
        <Button Content="Create" HorizontalAlignment="Right" Margin="10" Padding="2" Command="{Binding CreateCommand}" />
    </StackPanel>
</UserControl>
